/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:assign-tracer
 * Create Time: 2016年2月24日 下午1:56:01
 */
package com.lenovo.lmrp.server.assigntracer.facade;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lenovo.lmrp.api.constant.MessageTemplateType;
import com.lenovo.lmrp.api.model.entity.TicketPO;
import com.lenovo.lmrp.server.assigntracer.domain.ticket.TicketBO;
import com.lenovo.lmrp.server.assigntracer.repository.ARepositoryFactory;
import com.lenovo.sdc.framework.util.CommonUtil;

/**
 * 
 * @author wujin
 * @version v1.0
 */
public class TicketNotificationFacadeImpl implements ITicketNotificationFacade {

	private static Logger logger = LoggerFactory.getLogger(TicketNotificationFacadeImpl.class);

	/**
	 * 派单提醒工程师
	 * 
	 * @param ticket
	 */
	public void notifyToEngineer(String itsmCode, TicketPO ticket) {
		if (logger.isDebugEnabled()) {
			logger.debug("向工程师派单：itsmcode:{},ticketId:{},engineerId:{}", itsmCode, ticket.getId(),
					ticket.getAssignee());
		}
		try {
			TicketBO ticketBO = ARepositoryFactory.getFactory().getTicketRepository().createTicketBO(itsmCode, ticket);
			ticketBO.sendMessageOne();
		} catch (Exception e) {
			logger.error("向工程师派单操作异常：itsmcode:{},ticketId:{},engineerId:{},exception:{}", itsmCode, ticket.getId(),
					ticket.getAssignee(), e);
		}
	}

	/**
	 * 指定某一组工程师抢单
	 * 
	 * @param ticket
	 * @param retryCount
	 *            组号
	 */
	public void notifyToEngineerGroup(String itsmCode, TicketPO ticket, int retryCount) {
		if (logger.isDebugEnabled()) {
			logger.debug("抢单提醒：  itsmcode:{},ticketId:{},retryCount:{}", itsmCode, ticket.getId(), retryCount);
		}
		try {
			TicketBO ticketBO = ARepositoryFactory.getFactory().getTicketRepository().createTicketBO(itsmCode, ticket);
			ticketBO.sendMessageOnGroup(retryCount, MessageTemplateType.GRAB);
		} catch (Exception e) {
			logger.error("抢单提醒操作异常：  itsmcode:{},ticketId:{},retryCount:{}", itsmCode, ticket.getId(), retryCount, e);
		}
	}

	/**
	 * 通知场站管理员
	 * 
	 * @param ticket
	 */
	public void notifyToSiteManager(String itsmCode, TicketPO ticket) {
		if (logger.isDebugEnabled()) {
			logger.debug("通知场站管理员：    itsmcode:{},ticketId:{}", itsmCode, ticket.getId());
		}
		try {
			TicketBO ticketBO = ARepositoryFactory.getFactory().getTicketRepository().createTicketBO(itsmCode, ticket);
			ticketBO.SendMessageToSiteManager();
		} catch (Exception e) {
			logger.error("通知场站管理员操作异常：    itsmcode:{},ticketId:{},exception:{}", itsmCode, ticket.getId(), e);
		}
	}

	/**
	 * 抢单完成
	 * 
	 * @param ticket
	 * @param engineerID
	 */
	public void completeGrab(String itsmCode, TicketPO ticket, String engineerID) {
		if (CommonUtil.isNullOrEmpty(engineerID))
			return;

		if (logger.isDebugEnabled()) {
			logger.debug("抢单完成. ：itsmcode:{},ticketId:{},engineerID:{}", itsmCode, ticket.getId(), engineerID);
		}
		try {
			TicketBO ticketBO = ARepositoryFactory.getFactory().getTicketRepository()
					.createTicketBOwithMQClient(itsmCode, ticket, true);
			ticketBO.completeGrab(engineerID);
		} catch (Exception e) {
			logger.error("抢单完成操作异常. ：itsmcode:{},ticketId:{},engineerID:{},exception:{}", itsmCode, ticket.getId(),
					engineerID, e);
		}
	}

}
